package com.csun.cmny.provider.controller;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.OmcDisinfectionMethod;
import com.csun.cmny.provider.model.domain.OmcDisinfectionPosition;
import com.csun.cmny.provider.model.dto.disinfectionMethod.DisinfectionMethodSaveDto;
import com.csun.cmny.provider.model.dto.disinfectionMethod.DisinfectionMethodSelectDto;
import com.csun.cmny.provider.model.dto.disinfectionMethod.DisinfectionMethodUpdateDto;
import com.csun.cmny.provider.model.dto.disinfectionPosition.*;
import com.csun.cmny.provider.model.vo.disinfectionMethod.DisinfectionMethodVo;
import com.csun.cmny.provider.model.vo.disinfectionPosition.DisinfectionPositionVo;
import com.csun.cmny.provider.model.vo.disinfectionPosition.EverydayDisinfectionRecordVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcDisinfectionMethodService;
import com.csun.cmny.provider.service.OmcDisinfectionPositionService;
import com.csun.cmny.provider.service.OmcDisinfectionRecordPositionBindService;
import com.csun.cmny.provider.service.OmcEverydayDisinfectionRecordService;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping(value = "/disinfectionEveryday", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcDisinfectionEverydayController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private OmcDisinfectionMethodService methodService;

    @Resource
    private OmcDisinfectionPositionService positionService;

    @Resource
    private OmcDisinfectionRecordPositionBindService bindService;

    @Resource
    private OmcEverydayDisinfectionRecordService everydayDisinfectionRecordService;

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "新增消毒方式")
    @RequestMapping(value = "/addDisinfectionMethod", method = RequestMethod.POST)
    public Wrapper addChanner(@RequestBody @Valid DisinfectionMethodSaveDto disinfectionMethodSaveDto, BindingResult br) {

        OmcDisinfectionMethod omcDisinfectionMethod = new OmcDisinfectionMethod();

        BeanUtils.copyProperties(disinfectionMethodSaveDto,omcDisinfectionMethod);

        //判断此消毒方式是否已经存在
        List<OmcDisinfectionMethod> select = methodService.select(omcDisinfectionMethod);
        if (PublicUtil.isNotEmpty(select)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("disinfectionEveryday.addDisinfectionMethod.error", null));
        }

        omcDisinfectionMethod.setGroupId(getLoginAuthDto().getGroupId());
        omcDisinfectionMethod.setCreator(getLoginAuthDto().getUserName());
        omcDisinfectionMethod.setCreatorId(getLoginAuthDto().getUserId());

        int result = methodService.save(omcDisinfectionMethod);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null),result);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "修改消毒方式")
    @RequestMapping(value = "/updateDisinfectionMethod", method = RequestMethod.POST)
    public Wrapper updateDisinfectionMethod(@RequestBody @Valid DisinfectionMethodUpdateDto updateDto, BindingResult br) {

        //如果有绑定的消毒位置,不可修改
        OmcDisinfectionPosition omcDisinfectionPosition = new OmcDisinfectionPosition();

        omcDisinfectionPosition.setDisinfectionMethodId(updateDto.getMethodId());

        List<OmcDisinfectionPosition> list = positionService.select(omcDisinfectionPosition);

        if (PublicUtil.isNotEmpty(list)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("disinfectionMethod.update.error", null));
        }

        //判断此消毒方式是否已经存在
        OmcDisinfectionMethod omcDisinfectionMethod = new OmcDisinfectionMethod();

        BeanUtils.copyProperties(updateDto,omcDisinfectionMethod);

        List<OmcDisinfectionMethod> select = methodService.select(omcDisinfectionMethod);
        if (PublicUtil.isNotEmpty(select)){
            for (OmcDisinfectionMethod disinfectionMethod : select) {
                if (!disinfectionMethod.getId().equals(updateDto.getMethodId())){
                    return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("disinfectionEveryday.addDisinfectionMethod.error", null));
                }
            }
        }

        omcDisinfectionMethod.setId(updateDto.getMethodId());

        int result = methodService.update(omcDisinfectionMethod);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null),result);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "删除消毒方式")
    @RequestMapping(value = "/deleteDisinfectionMethod/{methodId}", method = RequestMethod.POST)
    public Wrapper deleteDisinfectionMethod(@PathVariable Long methodId) {

        //判断是否有绑定的消毒位置
        OmcDisinfectionPosition omcDisinfectionPosition = new OmcDisinfectionPosition();

        omcDisinfectionPosition.setDisinfectionMethodId(methodId);

        List<OmcDisinfectionPosition> select = positionService.select(omcDisinfectionPosition);

        if (PublicUtil.isNotEmpty(select)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("disinfectionMethod.delete.error", null));
        }

        int result = methodService.deleteByKey(methodId);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null),result);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "查看所有消毒方式")
    @RequestMapping(value = "/selectDisinfectionMethod", method = RequestMethod.POST)
    public Wrapper selectDisinfectionMethod(@RequestBody @Valid DisinfectionMethodSelectDto disinfectionMethodSelectDto, BindingResult br) {

        List<DisinfectionMethodVo>list=methodService.selectDisinfectionMethod(disinfectionMethodSelectDto);

        PageInfo<DisinfectionMethodVo> pageInfo = new PageInfo<>(list);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "新增消毒位置")
    @RequestMapping(value = "/addDisinfectionPosition", method = RequestMethod.POST)
    public Wrapper addDisinfectionPosition(@RequestBody @Valid DisinfectionPositionSaveDto disinfectionPositionSaveDto, BindingResult br) {

        OmcDisinfectionMethod omcDisinfectionMethod = methodService.selectByKey(disinfectionPositionSaveDto.getDisinfectionMethodId());
        if (PublicUtil.isEmpty(omcDisinfectionMethod)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("disinfectionEveryday.omcDisinfectionMethod.null", null));
        }

        OmcDisinfectionPosition omcDisinfectionPosition = new OmcDisinfectionPosition();

        BeanUtils.copyProperties(disinfectionPositionSaveDto,omcDisinfectionPosition);

        omcDisinfectionPosition.setGroupId(getLoginAuthDto().getGroupId());
        omcDisinfectionPosition.setCreator(getLoginAuthDto().getUserName());
        omcDisinfectionPosition.setCreatorId(getLoginAuthDto().getUserId());

        int result = positionService.save(omcDisinfectionPosition);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null),result);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "修改消毒位置")
    @RequestMapping(value = "/updateDisinfectionPosition", method = RequestMethod.POST)
    public Wrapper updateDisinfectionPosition(@RequestBody @Valid DisinfectionPositionUpdateDto updateDto, BindingResult br) {

        //判断此消毒方式是否存在
        OmcDisinfectionMethod omcDisinfectionMethod = methodService.selectByKey(updateDto.getDisinfectionMethodId());

        if (PublicUtil.isEmpty(omcDisinfectionMethod)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("disinfectionEveryday.omcDisinfectionMethod.null", null));
        }

        //修改消毒位置
        OmcDisinfectionPosition omcDisinfectionPosition = new OmcDisinfectionPosition();

        BeanUtils.copyProperties(updateDto,omcDisinfectionPosition);

        omcDisinfectionPosition.setId(updateDto.getPositionId());

        int result = positionService.update(omcDisinfectionPosition);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null),result);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "删除消毒位置")
    @RequestMapping(value = "/deleteDisinfectionPosition/{positionId}", method = RequestMethod.POST)
    public Wrapper deleteDisinfectionPosition(@PathVariable Long positionId) {

        int result = positionService.deleteByKey(positionId);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null),result);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "查看所有消毒位置")
    @RequestMapping(value = "/selectDisinfectionPosition", method = RequestMethod.POST)
    public Wrapper selectDisinfectionPosition(@RequestBody @Valid DisinfectionPositionSelectDto disinfectionPositionSelectDto, BindingResult br) {


        List<DisinfectionPositionVo>list=positionService.selectDisinfectionPosition(disinfectionPositionSelectDto);

        PageInfo<DisinfectionPositionVo> pageInfo = new PageInfo<>(list);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "新增日常消毒记录")
    @RequestMapping(value = "/addEverydayDisinfectionRecord", method = RequestMethod.POST)
    public Wrapper addEverydayDisinfectionRecord(@RequestBody @Valid EverydayDisinfectionRecordSaveDto everydayDisinfectionRecordSaveDto, BindingResult br) {


        int result=everydayDisinfectionRecordService.addEverydayDisinfectionRecord(everydayDisinfectionRecordSaveDto,getLoginAuthDto());


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null),result);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "修改日常消毒记录")
    @RequestMapping(value = "/updateEverydayDisinfectionRecord", method = RequestMethod.POST)
    public Wrapper updateEverydayDisinfectionRecord(@RequestBody @Valid EverydayDisinfectionRecordUpdateDto everydayDisinfectionRecordUpdateDto, BindingResult br) {


        int result=everydayDisinfectionRecordService.updateEverydayDisinfectionRecord(everydayDisinfectionRecordUpdateDto);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null),result);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "日常消毒记录分页查询")
    @RequestMapping(value = "/selectEverydayDisinfectionRecord", method = RequestMethod.POST)
    public Wrapper selectEverydayDisinfectionRecord(@RequestBody @Valid EverydayDisinfectionRecordSelectDto everydayDisinfectionRecordSelectDto, BindingResult br) {


        List<EverydayDisinfectionRecordVo> list=everydayDisinfectionRecordService.selectEverydayDisinfectionRecord(everydayDisinfectionRecordSelectDto);

        PageInfo<EverydayDisinfectionRecordVo> pageInfo = new PageInfo<>(list);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }
}
